/*
 * Copyright (c) 2010 Marc Poppleton
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.marcpoppleton.strawberry.view;

import org.marcpoppleton.strawberry.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;

public class Slider extends LinearLayout implements OnClickListener{
    
    private Button mButtonMinus;
    private Button mButtonPlus;
    private SeekBar mSeekbar;
    
    public Slider(Context context){
    	super(context);
    }
	
	public Slider(Context context, AttributeSet attrs) {
        super(context, attrs);
        
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Slider);
        final int max = a.getInt(R.styleable.Slider_max, 100);
        final int progress = a.getInt(R.styleable.Slider_progress, 50);
        final String sMin = a.getString(R.styleable.Slider_minus_button_text);
        final String sMax = a.getString(R.styleable.Slider_plus_button_text);        
        a.recycle();
        
        mButtonMinus = new Button(context);
        mButtonMinus.setText(sMin);        
        mButtonMinus.setOnClickListener(this);
        
        mButtonPlus = new Button(context);
        mButtonPlus.setText(sMax);
        mButtonPlus.setOnClickListener(this);
        
        mSeekbar = new SeekBar(context);
        mSeekbar.setThumbOffset(0);
        mSeekbar.setMax(max);
        mSeekbar.setProgress(progress);

        
        addView(mButtonMinus, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        addView(mSeekbar, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT,1));
        addView(mButtonPlus, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        
    }
    
	public void onClick(View v) {
		if((mButtonMinus == v)&&(mSeekbar.getProgress()>0)){
			mSeekbar.setProgress(mSeekbar.getProgress() - 1);
		}
		if((mButtonPlus == v)&&(mSeekbar.getProgress()<mSeekbar.getMax())){
			mSeekbar.setProgress(mSeekbar.getProgress() + 1);
		}
	}	 
	
	public void setOnSeekBarChangeListener(SeekBar.OnSeekBarChangeListener l){
		mSeekbar.setOnSeekBarChangeListener(l);
	}
	
	public int getProgress(){
		return mSeekbar.getProgress();
	}
	
	public void setMax(int max){
		mSeekbar.setMax(max);
	}
	
	public void setProgress(int progress){
		mSeekbar.setProgress(progress);
	}

}